package oakclub.model;

import oakclub.jooq.Tables;
import oakclub.jooq.tables.UserCoordinate;
import oakclub.jooq.tables.UserMapping;
import oakclub.jooq.tables.Users;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;

public class UserMappingModel extends BaseModel
{
    public void build() throws Exception
    {
        this.execute(new IExecute()
        {
            @Override
            public void execute(DSLContext context, Object... params) throws Exception
            {
                // table alias
                UserMapping UM = Tables.USER_MAPPING;
                Users U = Tables.USERS;
                UserCoordinate UC = Tables.USER_COORDINATE;

                // truncate
                context.truncate(UM).execute();

                // build
                context
                    .insertInto(UM, UM.USER_ID, UM.GENDER_ID, UM.YEAR_OF_BIRTH, UM.CITY_ID, UM.DISTANCE, UM.INTERESTED)
                    .select(context
                        .select(U.USER_ID, U.GENDER.cast(Byte.class), DSL.year(U.BIRTHDAY).cast(Short.class), UC.CITY_ID, UC.DISTANCE.cast(Short.class), U.INTERESTED.cast(Short.class))
                        .from(U)
                        .join(UC).on(UC.USER_ID.eq(U.USER_ID))
                        .where(U.IS_ENABLED.eq((byte) 1), U.APPROVED.eq((byte) 1), U.STATUS.eq(1)))
                    .execute();
            }
        });
    }
}
